/*
* @Description:合约管理-安装
* @Author: lil
* @Date: 2021/11/24
**/
<template>
  <div>
    <!-- 表单 -->
    <el-form :model="form" :rules="rules" ref="form" label-width="90px">
      <el-form-item label="选择节点:" prop="checkList1">
        <template v-if="peers.length>0">
          <el-checkbox-group v-model="form.checkList1">
            <el-checkbox v-for="data in peers" :label="data.label">
            </el-checkbox>
          </el-checkbox-group>
        </template>
        <template v-else>
          暂无节点
        </template>
      </el-form-item>
      <el-form-item label="选择通道:" prop="checkList2">
        <template v-if="channels.length>0">
          <el-checkbox-group v-model="form.checkList2">
            <el-checkbox v-for="data in channels" :label="data.label">
            </el-checkbox>
          </el-checkbox-group>
        </template>
        <template v-else>
          暂无通道
        </template>
      </el-form-item>
      <el-form-item label-width="0" class="btns">
        <el-button :disabled="peers.length==0" type="primary" @click="install">安装</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import {
    installChaincode,
    getPeerNameAndChannelNameByChaincode
  } from '@/api/blockchain/blockchaindeploy'
  export default {
    props: ['versionCode', 'chainCodeName'],
    data() {
      return {
        peers: [],
        channels: [],
        form: {
          checkList1: [],
          checkList2: [],
        },
        rules: {
          checkList1: [{
            type: 'array',
            required: true,
            message: '请至少选择一个节点！',
            trigger: 'change'
          }],
          checkList2: [{
            type: 'array',
            required: true,
            message: '请至少选择一个通道！',
            trigger: 'change'
          }],
        }
      };
    },

    mounted() {
      var _this = this;
      getPeerNameAndChannelNameByChaincode(_this.chainCodeName, _this.versionCode).then(function (res) {
        _this.peers = [];
        for (let i = 0; i < res.data.peer.length; i++) {
          _this.peers.push({
            label: ''
          });
          _this.peers[i].label = res.data.peer[i];
        }
        _this.channels = [];
        for (let i = 0; i < res.data.channel.length; i++) {
          _this.channels.push({
            label: ''
          });
          _this.channels[i].label = res.data.channel[i];
        }
      }).catch((err) => {
        console.log(err);
      });
    },

    methods: {

      install() {
        let data1 = '';
        for (let item of this.form.checkList1) {
          if (data1.length == 0) {
            data1 = data1 + item;
          } else {
            data1 = data1 + "," + item;
          }
        }
        let channelNames = '';
        for (let item of this.form.checkList2) {
          if (channelNames.length == 0) {
            channelNames = channelNames + item;
          } else {
            channelNames = channelNames + "," + item;
          }
        }
        this.$refs.form.validate((valid) => {
          if (valid) {
            installChaincode(data1, channelNames, this.chainCodeName, this.versionCode).then((res) => {
              this.$message.success("合约安装成功！");
              this.$emit('close')
            }).catch((err) => {
              console.log(err);
            });
          }
        })
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-form {
    padding: 40px 40px 10px;

    &>>>.el-form-item__label {
      &::before {
        content: '*';
        display: inline-block;
        color: #f12945;
      }
    }

    .btns {
      text-align: center;
      margin-top: 70px;

      .el-button:nth-of-type(1) {
        margin-right: 40px;
      }
    }
  }

</style>
